﻿#region Imports
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Data;
using BulkRenamer.Common.Models;
using CommonUtilities.Extensions;
#endregion Imports

namespace BulkRenamer.App.WPF.Converters
{
    public class FileDetailsConverter : IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string option = (string)parameter;
            string fileDetails = (string)value; // value.Coalesce( null : (FileDetails)value;
            if (fileDetails.IsBlank()) fileDetails = null;

            switch (option)
            {
                case "LogPath": return fileDetails.Coalesce("<log path>");
                case "BasePath": return fileDetails.Coalesce("<base directory>");

                case "Pattern": return fileDetails.Coalesce("<chng_pattern>");
                case "Append": return fileDetails.Coalesce("<append>");
                case "Replace": return fileDetails.Coalesce("<replace>");

                case "NameOrg": return fileDetails.Coalesce("<original name>");
                case "NameSugg": return fileDetails.Coalesce("<suggested name>");
                case "NameCust": return fileDetails.Coalesce("<custom name>");
                case "FileExtn": return fileDetails.Coalesce("<extn>");

                case "DataMgmt": return fileDetails.Coalesce("<path for import/export in bulk>");

                default: throw new NotImplementedException();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //return
            //value.ToString().ToCharArray().Join<char, char, char, char>(
            //    Path.GetInvalidFileNameChars(), o => o, i => i, res => res).ToList(); //.LongCount()>0 ? null : value;

            if ("NameOrg,NameSugg,NameCust,FileExtn".Split(',')._Contains(parameter))
                foreach (char chr in value.ToString().ToCharArray())
                    if (Path.GetInvalidFileNameChars().Contains(chr)) return null;

            return value;
        }
        #endregion IValueConverter Members
    }
}
